<template>
  <div class="article-page">
    <nav class="my-nav van-hairline--bottom">
      <!-- axios在携带参数的时候,如果参数是null或undefined,则不携带 -->
      <a
        href="javascript:;"
        @click="getlist('weight_desc')"
        :class="{active: sorter === 'weight_desc'}"
        >推荐</a
      >
      <a
        href="javascript:;"
         @click="getlist(null)"
        :class="{active: sorter === null}"
        >最新</a
      >
      <div class="logo"><img src="@/assets/logo.png" alt=""></div>
    </nav>
<van-list
  v-model="loading"
  :finished="finished"
  finished-text="没有更多了"
  @load="onLoad"
>
   <article-item v-for="item in list" :key="item.id" :item="item"></article-item>
</van-list>
  </div>
</template>

<script>
import { reqGetArticleList } from '@/utils/article'
import articleItem from '@/components/article-item.vue'
export default {
  components: { articleItem },
  name: 'article-page',
  async created () {
    // const { data: { rows } } = await reqGetArticleList({ current: 1, pageSize: 10, sorter: 'weight_desc' })
    // this.list = rows
  },
  data () {
    return {
      list: [],
      loading: false,
      finished: false,
      current: 1,
      sorter: 'weight_desc'
    }
  },
  methods: {
    // 数据未沾满屏幕 ||滚动触底 触发onload
    async onLoad () { // 上来就会执行一次
      // console.log('获取数据')
      const { data: { rows, pageTotal } } = await reqGetArticleList({ current: this.current, pageSize: 10, sorter: this.sorter })
      this.list = [...this.list, ...rows]
      this.current++ // 取下一页的数据
      // 手动加loading设置为false
      this.loading = false
      // 如果所有数据加载完了设置finished为true
      if (this.current > pageTotal) {
        this.finished = true
      }
    },
    getlist (value) {
      this.sorter = value// 修改sorter
      this.loading = true// 手动开启loading发送axios请求
      this.current = 1// 从第一页开始获取最新的数据
      this.list = []// 清空数组里的数据,为下一次盛放新的数组做准备
      this.finished = false// 导致切换面板只能取一次数据就结束的问题
      this.onLoad()
    }
  }
}
</script>

<style lang="less" scoped>
.article-page {
  margin-bottom: 50px;
  margin-top: 44px;
  .my-nav {
    height: 44px;
    position: fixed;
    left: 0;
    top: 0;
    width: 100%;
    z-index: 999;
    background: #fff;
    display: flex;
    align-items: center;
    > a {
      color: #999;
      font-size: 14px;
      line-height: 44px;
      margin-left: 20px;
      position: relative;
      transition: all 0.3s;
      &::after {
        content: '';
        position: absolute;
        left: 50%;
        transform: translateX(-50%);
        bottom: 0;
        width: 0;
        height: 2px;
        background: #222;
        transition: all 0.3s;
      }
      &.active {
        color: #222;
        &::after {
          width: 14px;
        }
      }
    }
    .logo {
      flex: 1;
      display: flex;
      justify-content: flex-end;
      > img {
        width: 64px;
        height: 28px;
        display: block;
        margin-right: 10px;
      }
    }
  }
}

</style>
